#include "unp.h"

int main(int argc, char *argv[])
{
    int srvfd, connfd, addrlen;
    struct sockaddr_in srvaddr, cliaddr;
    extern void sig_child(int);

    srvfd = Socket(AF_INET, SOCK_STREAM, 0);

    bzero(&srvaddr, sizeof(srvaddr));
    srvaddr.sin_family = AF_INET;
    srvaddr.sin_port = htons(SERV_PORT);
    srvaddr.sin_addr.s_addr = htonl(INADDR_ANY);

    Bind(srvfd, (SA*)&srvaddr, sizeof(srvaddr));
    Listen(srvfd, 15);

    signal(SIGCHLD, sig_child); /* call waitpid */
    
    while (1) {
        connfd = Accept(srvfd, (SA*)&cliaddr, &addrlen);
        if (fork() == 0) {
            Close(srvfd);
            str_echo(connfd);
            exit(0);
        }
        Close(connfd);
    }
    
    return 0;
}
